/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.gamestates;

import bungabungaball.GlobalSettings;
import bungabungaball.gamestates.menu.Menu;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bungabungaball.gamestates.menu.MenuEntry;
import org.jbox2d.common.Vec2;
import org.newdawn.slick.Font;
import org.newdawn.slick.Image;
import org.newdawn.slick.TrueTypeFont;

/**
 * The main menu
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class MainMenu extends BasicGameState implements Menu {

    /**
     * the id of the game state
     */
    private int stateID = -1;
    /**
     * *
     * the current mouse position x
     */
    private int mouseX;
    /**
     * *
     * the current mouse position y
     */
    private int mouseY;
    /**
     * a menu entry to start a single player game
     */
    private MenuEntry startSinglePlayerGame;
    /**
     * a menu entry to start a multi player game
     */
    private MenuEntry startMultiPlayerGame;
    /**
     * a menu entry to start a single player game; specified
     */
    private MenuEntry startSinglePlayerSpecificGame;
    /**
     * a menu entry to start a multi player game; specified
     */
    private MenuEntry startMultiPlayerSpecificGame;
    /**
     * a menu entry to create a network game (be the server)
     */
    private MenuEntry createMenuEntry;
    /**
     * a menu entry to join a network game (be the client)
     */
    private MenuEntry joinNetzworkGame;
    /**
     * a menu entry to start the options
     */
    private MenuEntry startOptions;
    /**
     * a menu entry to end BungaBungaBall
     */
    private MenuEntry endApplication;
    /**
     * a menu entry to start the level editor
     */
    private MenuEntry startLevelEditor;
    /**
     * the font
     */
    public static Font font = new TrueTypeFont(new java.awt.Font("Cooper Black", 0, 18), false);
    /**
     * An image of the driver
     */
    private Image driver;
    /**
     * An image of the driver
     */
    private Image driver2;
    /**
     * Bunga Logo
     */
    private Image bunga;
    /**
     * Bunga Logo
     */
    private Image bungamenu;
    /**
     * x Position of the driver
     */
    private double driverX;
    /**
     * Y Position of the driver
     */
    private double driverY;
    /**
     * A counter for the driver
     */
    private double counter = 0;
    /**
     * A checker for the driver
     */
    private boolean checker = true;

    /**
     * Constructor
     *
     * @param stateID
     */
    public MainMenu(int stateID) {
        this.stateID = stateID;
        driverX = 600;
        driverY = 350;
    }

    /**
     * Get the id of the game state
     *
     * @return id of the state
     */
    @Override
    public int getID() {
        return stateID;
    }

    /**
     * create all menu entries
     *
     * @param gc
     * @param sbg
     * @throws SlickException
     */
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {



        startSinglePlayerGame = new MenuEntry("Schnelles Spiel", 100, 150, gc.getDefaultFont());

        startMultiPlayerGame = new MenuEntry("Schnelles Mehrspielerspiel", 100, 180, gc.getDefaultFont());

        startSinglePlayerSpecificGame = new MenuEntry("Benutzerdefiniertes Einzelspiel", 100, 210, gc.getDefaultFont());

        startMultiPlayerSpecificGame = new MenuEntry("Benutzerdefiniertes Mehrspielerspiel", 100, 240, gc.getDefaultFont());

        createMenuEntry = new MenuEntry("Netzwerkspiel einleiten", 100, 270, gc.getDefaultFont());

        joinNetzworkGame = new MenuEntry("An Netzwerkspiel teilnehmen", 100, 300, gc.getDefaultFont());

        startLevelEditor = new MenuEntry("Level-Editor starten", 100, 330, gc.getDefaultFont());

        startOptions = new MenuEntry("Optionen", 100, 360, gc.getDefaultFont());

        endApplication = new MenuEntry("Beenden", 100, 420, gc.getDefaultFont());

        driver = new Image("\\img\\player1.png");

        driver2 = new Image("\\img\\player2.png");
        driver2.setRotation(45);

        bunga = new Image("\\img\\bungabunga.png");

        bungamenu = new Image("\\img\\bungamenu.png");
    }

    /**
     * render the menu
     *
     * @param gc
     * @param sbg
     * @param g
     * @throws SlickException
     */
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setColor(Color.red);
        g.setBackground(Color.black);
        g.setFont(font);
        //g.drawString("Menü", 100, 100);
        g.setColor(startSinglePlayerGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startSinglePlayerGame.text, startSinglePlayerGame.x, startSinglePlayerGame.y);

        g.setColor(startMultiPlayerGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startMultiPlayerGame.text, startMultiPlayerGame.x, startMultiPlayerGame.y);

        g.setColor(startSinglePlayerSpecificGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startSinglePlayerSpecificGame.text, startSinglePlayerSpecificGame.x, startSinglePlayerSpecificGame.y);

        g.setColor(startMultiPlayerSpecificGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startMultiPlayerSpecificGame.text, startMultiPlayerSpecificGame.x, startMultiPlayerSpecificGame.y);

        g.setColor(createMenuEntry.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(createMenuEntry.text, createMenuEntry.x, createMenuEntry.y);

        g.setColor(joinNetzworkGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(joinNetzworkGame.text, joinNetzworkGame.x, joinNetzworkGame.y);

        g.setColor(startLevelEditor.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startLevelEditor.text, startLevelEditor.x, startLevelEditor.y);

//        g.setColor(startSinglePlayerGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
//        g.drawString(startSinglePlayerGame.text, startSinglePlayerGame.x, startSinglePlayerGame.y);
//
//        g.setColor(startSinglePlayerGame.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
//        g.drawString(startSinglePlayerGame.text, startSinglePlayerGame.x, startSinglePlayerGame.y);

        g.setColor(startOptions.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(startOptions.text, startOptions.x, startOptions.y);

        g.setColor(endApplication.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(endApplication.text, endApplication.x, endApplication.y);

        g.drawImage(driver, (int) driverX, (int) driverY);

        g.drawImage(driver2, 200, 400);


        g.drawImage(bunga, 400, 0);
        g.drawImage(bungamenu, 85, 80);

    }

    /**
     * handle all input
     *
     * @param gc
     * @param sbg
     * @param delta
     * @throws SlickException
     */
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();
        /*
         if (input.isKeyDown(Input.KEY_A) || input.isKeyDown(Input.KEY_LEFT)) {
         sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
         }
         */
        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            if (startSinglePlayerGame.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.SinglePlayer;
                startSinglePlayerGame.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
            }
            if (startMultiPlayerGame.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.MultiPlayer;
                startMultiPlayerGame.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESingle);
            }
            if (startSinglePlayerSpecificGame.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.SinglePlayer;
                startSinglePlayerSpecificGame.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESpecific);
            }
            if (startMultiPlayerSpecificGame.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.MultiPlayer;
                startMultiPlayerSpecificGame.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESpecific);
            }
            if (createMenuEntry.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.Server;
                createMenuEntry.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATESTARTNETWORK);
            }
            if (joinNetzworkGame.checkMouseIn(mouseX, mouseY)) {
                GlobalSettings.currentGameMode = GlobalSettings.gameModes.Client;
                joinNetzworkGame.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.GAMEPLAYSTATEJOINNETWORK);
            }
            if (startLevelEditor.checkMouseIn(mouseX, mouseY)) {

                startLevelEditor.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.LEVELEDITOR);


            }
            if (startOptions.checkMouseIn(mouseX, mouseY)) {
                sbg.enterState(bungabungaball.BungaBungaBall.OPTIONPLAYSTATE);
            }

            if (endApplication.checkMouseIn(mouseX, mouseY)) {
                gc.exit();
            }
        }
        if (input.isKeyDown(Input.KEY_ESCAPE)) {
            gc.exit();
        }

        mouseX = input.getAbsoluteMouseX();
        mouseY = input.getAbsoluteMouseY();

        this.drive();
        this.drive();
    }

    /**
     * Updates the driver Image
     */
    public void drive() {
        //x = 600 y = 400
        double speedRotation = Math.toRadians(driver.getRotation() - 90.f);

        driverX = driverX + 2 * Math.cos(speedRotation);
        driverY = driverY + 2 * Math.sin(speedRotation);

        if (checker && counter <= 270) {
            driver.rotate(1);
        } else if (!checker && counter <= 270) {
            driver.rotate(-1);
        }
        counter++;
        if (counter == 385) {
            counter = 0;
            if (checker) {
                checker = false;
            } else {
                checker = true;
            }
        }
    }
}
